<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="500px"
    top="5vh"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" v-if="!isEdit">
        <el-input v-model="form.password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar_url">
        <FileUpload v-model="form.avatar_url"></FileUpload>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" :active-value="1" :inactive-value="0" active-text="正常" inactive-text="禁用"></el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";
import FileUpload from "@/components/UploadFile.vue";

export default {
  name: "MemberAdd",
  components: {FileUpload},
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "新增会员",
      isEdit: false,
      form: {
        phone: "",
        nickname: "",
        password: "",
        avatar_url: "",
        status: 1
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { max: 50, message: "姓名不能超过50个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, message: "密码不能少于6个字符", trigger: "blur" }
        ],
        avatar_url: [
          { max: 255, message: "头像URL不能超过255个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    open(row) {
      this.dialogVisible = true;
      if (row) {
        this.isEdit = true;
        this.dialogTitle = "编辑会员";
        http.user.read(row.id).then(res => {
          this.form = {
            ...res.data,
          }
        })
      } else {
        this.isEdit = false;
        this.dialogTitle = "新增会员";
        this.form = {
          phone: "",
          nickname: "",
          password: "",
          avatar_url: "",
          status: 1
        };
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            if (this.isEdit) {
              // 编辑时不更新密码
              const { password, ...updateData } = this.form;
              await http.user.update(this.form.id, updateData);
              this.$message.success("更新成功");
            } else {
              await http.user.create(null, this.form);
              this.$message.success("新增成功");
            }
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            this.$message.error("操作失败");
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
.tip-text {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}
</style>
